package com.hzlj.position.locate.dao;

import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fz.common.base.converter.QueryWrapperConverterPlus;
import com.fz.common.base.core.JsonUtils;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.hzlj.position.locate.common.dto.reportAudio.ReportAudioHandleDTO;
import com.hzlj.position.locate.common.dto.reportAudio.ReportAudioHandleQueryResultDTO;
import com.hzlj.position.locate.common.dto.reportAudio.ReportAudioQueryParamDTO;
import com.hzlj.position.locate.common.dto.reportAudio.ReportAudioWithJzJbxxDTO;
import com.hzlj.position.locate.common.model.ReportAudio;
import com.hzlj.position.locate.mapper.ReportAudioMapper;
import com.hzlj.sqjz.jz.common.constant.JzJoin;
import com.hzlj.sqjz.jz.common.model.JzJbxx;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 主动音频上报(ReportAudio)表操作类
 *
 * @author makejava
 * @date 2024-01-15 10:49:16
 */
@Repository
public class ReportAudioDao extends ServiceImpl<ReportAudioMapper, ReportAudio> {
    @Resource
    private ReportAudioMapper reportAudioMapper;


    /**
     * 修改数据
     */
    public boolean updateReportAudio(ReportAudio audio) {
        LambdaUpdateWrapper<ReportAudio> updateWrapper = new UpdateWrapper<ReportAudio>().lambda()
                .eq(ReportAudio::getId, audio.getId());
        return this.update(audio, updateWrapper);

    }


    /**
     * 查询列表：不分页
     */
    public List<ReportAudioWithJzJbxxDTO> listReportAudio(ReportAudioQueryParamDTO dto) {
        return reportAudioQuery()
                .list(ReportAudioWithJzJbxxDTO.class, dto);
    }

    /**
     * 查询列表：分页
     */
    public QueryPageResultDTO<ReportAudioWithJzJbxxDTO> pageReportAudio(ReportAudioQueryParamDTO dto, PageParamDTO page) {
        return reportAudioQuery()
                .page(ReportAudioWithJzJbxxDTO.class, dto, page);
    }

    public ReportAudioHandleQueryResultDTO getHandleResult(String id, Date belongDay) {
        return QueryWrapperConverterPlus.of(reportAudioMapper)
                .selectOne(ReportAudioHandleQueryResultDTO.class, null, null, q -> {
                    q.eq(ReportAudio::getId, id)
                            .eq(ReportAudio::getBelongDay, belongDay);
                });
    }


    public void handle(ReportAudioHandleDTO dto) {
        Pair<Date, Date> dateDatePair = dto.minMaxBelongDay();
        LambdaUpdateWrapper<ReportAudio> updateWrapper = new UpdateWrapper<ReportAudio>().lambda()
                .set(ReportAudio::getHandleVoiceprintMatch, dto.getHandleVoiceprintMatch())
                .set(ReportAudio::getHandleReportStatus, dto.getHandleReportStatus())
                .set(ReportAudio::getHandleStatus, dto.getHandleStatus())
                .set(ReportAudio::getHandleTime, dto.getHandleTime())
                .set(ObjectUtil.isNotEmpty(dto.getHandleExt()), ReportAudio::getHandleExt, JsonUtils.obj2json(dto.getHandleExt()))
                .in(ReportAudio::getId, dto.ids())
                .ge(ReportAudio::getBelongDay, dateDatePair.getKey())
                .le(ReportAudio::getBelongDay, dateDatePair.getValue());
        this.update(updateWrapper);
    }


    private QueryWrapperConverterPlus<ReportAudio> reportAudioQuery() {
        return QueryWrapperConverterPlus.of(reportAudioMapper)
                .innerJoin(JzJbxx.class, JzJoin.zj(ReportAudio::getJzId));
    }


}

